//
//  ViewController.m
//  DemoProject
//
//  Created by hao on 2018/7/10.
//  Copyright © 2018年 hao. All rights reserved.
//

#import "UserNotifiViewController.h"
#import <UserNotifications/UserNotifications.h>

@interface UserNotifiViewController ()

@end

@implementation UserNotifiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor orangeColor];
//    NSLog(@"%d", 1/(arc4random()%2));
//    NSLog(@"%@", NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject);
    [self setupView];
}

- (void)setupView
{
    UIButton *addNotifiBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:addNotifiBtn];
    [addNotifiBtn setTitle:@"添加一条本地通知,3秒后提示" forState:UIControlStateNormal];
    addNotifiBtn.frame = CGRectMake(0, 0, 200, 34);
    addNotifiBtn.center = CGPointMake(self.view.frame.size.width*0.5, self.view.frame.size.height*0.5);
    [addNotifiBtn addTarget:self action:@selector(addUserNotifi) forControlEvents:UIControlEventTouchUpInside];
}

- (void)addUserNotifi
{
    if (@available(iOS 10.0, *)) {
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
        content.title = @"我是通知的标题";
        content.subtitle = @"副标题";
        content.body = @"通知正文";
        content.badge = @1;
        
        UNNotificationSound *sound = [UNNotificationSound defaultSound];
        content.sound = sound;
        content.categoryIdentifier = @"UNNotificationCategoryIdentifier";
        
        // 设置推送时间
        UNTimeIntervalNotificationTrigger *timeTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"UserNotificationIdentifier" content:content trigger:timeTrigger];
        
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (error) {
                NSLog(@"addNotificationRequest error -> %@", error);
            }
        }];
    }
}

@end
